<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>matrix_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_math_matrix_test.js.html">matrix_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.math.MatrixTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.math.MatrixTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.math.Matrix&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>function testConstuctorWithGoodArray() {
<a name="line22"></a>  var a1 = [[1, 2], [2, 3], [4, 5]];
<a name="line23"></a>  var m1 = new goog.math.Matrix(a1);
<a name="line24"></a>  assertArrayEquals(&#39;1. Internal array should be the same&#39;, m1.toArray(), a1);
<a name="line25"></a>  assertEquals(3, m1.getSize().height);
<a name="line26"></a>  assertEquals(2, m1.getSize().width);
<a name="line27"></a>
<a name="line28"></a>  var a2 = [[-61, 45, 123], [11112, 343, 1235]];
<a name="line29"></a>  var m2 = new goog.math.Matrix(a2);
<a name="line30"></a>  assertArrayEquals(&#39;2. Internal array should be the same&#39;, m2.toArray(), a2);
<a name="line31"></a>  assertEquals(2, m2.getSize().height);
<a name="line32"></a>  assertEquals(3, m2.getSize().width);
<a name="line33"></a>
<a name="line34"></a>  var a3 = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]];
<a name="line35"></a>  var m3 = new goog.math.Matrix(a3);
<a name="line36"></a>  assertArrayEquals(&#39;3. Internal array should be the same&#39;, m3.toArray(), a3);
<a name="line37"></a>  assertEquals(4, m3.getSize().height);
<a name="line38"></a>  assertEquals(4, m3.getSize().width);
<a name="line39"></a>}
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>function testConstructorWithBadArray() {
<a name="line43"></a>  assertThrows(&#39;1. All arrays should be of equal length&#39;, function() {
<a name="line44"></a>    new goog.math.Matrix([[1, 2, 3], [1, 2], [1]]);
<a name="line45"></a>  });
<a name="line46"></a>
<a name="line47"></a>  assertThrows(&#39;2. All arrays should be of equal length&#39;, function() {
<a name="line48"></a>    new goog.math.Matrix([[1, 2], [1, 2], [1, 2, 3, 4]]);
<a name="line49"></a>  });
<a name="line50"></a>
<a name="line51"></a>  assertThrows(&#39;3. Arrays should contain only numeric values&#39;, function() {
<a name="line52"></a>    new goog.math.Matrix([[1, 2], [1, 2], [1, &#39;a&#39;]]);
<a name="line53"></a>  });
<a name="line54"></a>
<a name="line55"></a>  assertThrows(&#39;4. Arrays should contain only numeric values&#39;, function() {
<a name="line56"></a>    new goog.math.Matrix([[1, 2], [1, 2], [1, {a: 3}]]);
<a name="line57"></a>  });
<a name="line58"></a>
<a name="line59"></a>  assertThrows(&#39;5. Arrays should contain only numeric values&#39;, function() {
<a name="line60"></a>    new goog.math.Matrix([[1, 2], [1, 2], [1, [1, 2, 3]]]);
<a name="line61"></a>  });
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>function testConstructorWithGoodNumbers() {
<a name="line66"></a>  var m1 = new goog.math.Matrix(2, 2);
<a name="line67"></a>  assertEquals(&#39;Height should be 2&#39;, 2, m1.getSize().height);
<a name="line68"></a>  assertEquals(&#39;Width should be 2&#39;, 2, m1.getSize().width);
<a name="line69"></a>
<a name="line70"></a>  var m2 = new goog.math.Matrix(4, 2);
<a name="line71"></a>  assertEquals(&#39;Height should be 4&#39;, 4, m2.getSize().height);
<a name="line72"></a>  assertEquals(&#39;Width should be 2&#39;, 2, m2.getSize().width);
<a name="line73"></a>
<a name="line74"></a>  var m3 = new goog.math.Matrix(4, 6);
<a name="line75"></a>  assertEquals(&#39;Height should be 4&#39;, 4, m3.getSize().height);
<a name="line76"></a>  assertEquals(&#39;Width should be 6&#39;, 6, m3.getSize().width);
<a name="line77"></a>}
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>function testConstructorWithBadNumbers() {
<a name="line81"></a>  assertThrows(&#39;1. Negative argument should have errored&#39;, function() {
<a name="line82"></a>    new goog.math.Matrix(-4, 6);
<a name="line83"></a>  });
<a name="line84"></a>
<a name="line85"></a>  assertThrows(&#39;2. Negative argument should have errored&#39;, function() {
<a name="line86"></a>    new goog.math.Matrix(4, -6);
<a name="line87"></a>  });
<a name="line88"></a>
<a name="line89"></a>  assertThrows(&#39;3. Zero argument should have errored&#39;, function() {
<a name="line90"></a>    new goog.math.Matrix(4, 0);
<a name="line91"></a>  });
<a name="line92"></a>
<a name="line93"></a>  assertThrows(&#39;4. Zero argument should have errored&#39;, function() {
<a name="line94"></a>    new goog.math.Matrix(0, 1);
<a name="line95"></a>  });
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>function testConstructorWithMatrix() {
<a name="line100"></a>  var a1 = [[1, 2], [2, 3], [4, 5]];
<a name="line101"></a>  var m1 = new goog.math.Matrix(a1);
<a name="line102"></a>  var m2 = new goog.math.Matrix(m1);
<a name="line103"></a>  assertArrayEquals(
<a name="line104"></a>      &#39;Internal arrays should be the same&#39;, m1.toArray(), m2.toArray());
<a name="line105"></a>  assertNotEquals(
<a name="line106"></a>      &#39;Should be different objects&#39;, goog.getUid(m1), goog.getUid(m2));
<a name="line107"></a>}
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>function testCreateIdentityMatrix() {
<a name="line111"></a>  var m1 = goog.math.Matrix.createIdentityMatrix(3);
<a name="line112"></a>  assertArrayEquals([[1, 0, 0], [0, 1, 0], [0, 0, 1]], m1.toArray());
<a name="line113"></a>
<a name="line114"></a>  var m2 = goog.math.Matrix.createIdentityMatrix(4);
<a name="line115"></a>  assertArrayEquals(
<a name="line116"></a>      [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], m2.toArray());
<a name="line117"></a>}
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>function testIsValidArrayWithGoodArrays() {
<a name="line121"></a>  var fn = goog.math.Matrix.isValidArray;
<a name="line122"></a>  assertTrue(&#39;2x2 array should be fine&#39;, fn([[1, 2], [3, 5]]));
<a name="line123"></a>  assertTrue(&#39;3x2 array should be fine&#39;, fn([[1, 2, 3], [3, 5, 6]]));
<a name="line124"></a>  assertTrue(
<a name="line125"></a>      &#39;3x3 array should be fine&#39;, fn([[1, 2, 3], [3, 5, 6], [10, 10, 10]]));
<a name="line126"></a>  assertTrue(&#39;[[1]] should be fine&#39;, fn([[1]]));
<a name="line127"></a>  assertTrue(&#39;1D array should work&#39;, fn([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]));
<a name="line128"></a>  assertTrue(&#39;Negs and decimals should be ok&#39;,
<a name="line129"></a>      fn([[0], [-4], [-10], [1.2345], [123.53]]));
<a name="line130"></a>  assertTrue(&#39;Hex, Es and decimals are ok&#39;, fn([[0x100, 10E-2], [1.213, 213]]));
<a name="line131"></a>}
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>function testIsValidArrayWithBadArrays() {
<a name="line135"></a>  var fn = goog.math.Matrix.isValidArray;
<a name="line136"></a>  assertFalse(&#39;Arrays should have same size&#39;, fn([[1, 2], [3]]));
<a name="line137"></a>  assertFalse(&#39;Arrays should have same size 2&#39;, fn([[1, 2], [3, 4, 5]]));
<a name="line138"></a>  assertFalse(&#39;2D arrays are ok&#39;, fn([[1, 2], [3, 4], []]));
<a name="line139"></a>  assertFalse(&#39;Values should be numeric&#39;, fn([[1, 2], [3, &#39;a&#39;]]));
<a name="line140"></a>  assertFalse(&#39;Values can not be strings&#39;, fn([[&#39;bah&#39;], [&#39;foo&#39;]]));
<a name="line141"></a>  assertFalse(&#39;Flat array not supported&#39;, fn([1, 2, 3, 4, 5]));
<a name="line142"></a>}
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>function testForEach() {
<a name="line146"></a>  var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line147"></a>  var count = 0, sum = 0, xs = &#39;&#39;, ys = &#39;&#39;;
<a name="line148"></a>  goog.math.Matrix.forEach(m, function(val, x, y) {
<a name="line149"></a>    count++;
<a name="line150"></a>    sum += val;
<a name="line151"></a>    xs += x;
<a name="line152"></a>    ys += y;
<a name="line153"></a>  });
<a name="line154"></a>  assertEquals(&#39;forEach should have visited every item&#39;, 9, count);
<a name="line155"></a>  assertEquals(&#39;forEach should have summed all values&#39;, 45, sum);
<a name="line156"></a>  assertEquals(&#39;Xs should have been visited in order&#39;, &#39;000111222&#39;, xs);
<a name="line157"></a>  assertEquals(&#39;Ys should have been visited sequentially&#39;, &#39;012012012&#39;, ys);
<a name="line158"></a>}
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>function testMap() {
<a name="line162"></a>  var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line163"></a>  var m2 = goog.math.Matrix.map(m1, function(val, x, y) {
<a name="line164"></a>    return val + 1;
<a name="line165"></a>  });
<a name="line166"></a>  assertArrayEquals([[2, 3, 4], [5, 6, 7], [8, 9, 10]], m2.toArray());
<a name="line167"></a>}
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>function testSetValueAt() {
<a name="line171"></a>  var m = new goog.math.Matrix(3, 3);
<a name="line172"></a>  for (var x = 0; x &lt; 3; x++) {
<a name="line173"></a>    for (var y = 0; y &lt; 3; y++) {
<a name="line174"></a>      m.setValueAt(x, y, 3 * x - y);
<a name="line175"></a>    }
<a name="line176"></a>  }
<a name="line177"></a>  assertArrayEquals([[0, -1, -2], [3, 2, 1], [6, 5, 4]], m.toArray());
<a name="line178"></a>}
<a name="line179"></a>
<a name="line180"></a>
<a name="line181"></a>function testGetValueAt() {
<a name="line182"></a>  var m = new goog.math.Matrix([[0, -1, -2], [3, 2, 1], [6, 5, 4]]);
<a name="line183"></a>  for (var x = 0; x &lt; 3; x++) {
<a name="line184"></a>    for (var y = 0; y &lt; 3; y++) {
<a name="line185"></a>      assertEquals(
<a name="line186"></a>          &#39;Value at (x, y) should equal 3x - y&#39;,
<a name="line187"></a>          3 * x - y, m.getValueAt(x, y));
<a name="line188"></a>    }
<a name="line189"></a>  }
<a name="line190"></a>  assertNull(&#39;Out of bounds value should be null&#39;, m.getValueAt(-1, 2));
<a name="line191"></a>  assertNull(&#39;Out of bounds value should be null&#39;, m.getValueAt(-1, 0));
<a name="line192"></a>  assertNull(&#39;Out of bounds value should be null&#39;, m.getValueAt(0, 4));
<a name="line193"></a>}
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>function testSum1() {
<a name="line197"></a>  var m1 = new goog.math.Matrix([[1, 1, 1], [2, 2, 2], [3, 3, 3]]);
<a name="line198"></a>  var m2 = new goog.math.Matrix([[3, 3, 3], [2, 2, 2], [1, 1, 1]]);
<a name="line199"></a>  assertArrayEquals(&#39;Sum should be all the 4s&#39;,
<a name="line200"></a>      [[4, 4, 4], [4, 4, 4], [4, 4, 4]], m1.add(m2).toArray());
<a name="line201"></a>  assertArrayEquals(&#39;Addition should be commutative&#39;,
<a name="line202"></a>      m1.add(m2).toArray(), m2.add(m1).toArray());
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>
<a name="line206"></a>function testSum2() {
<a name="line207"></a>  var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line208"></a>  var m2 = new goog.math.Matrix([[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]]);
<a name="line209"></a>  assertArrayEquals(&#39;Sum should be all 0s&#39;,
<a name="line210"></a>      [[0, 0, 0], [0, 0, 0], [0, 0, 0]], m1.add(m2).toArray());
<a name="line211"></a>  assertArrayEquals(&#39;Addition should be commutative&#39;,
<a name="line212"></a>      m1.add(m2).toArray(), m2.add(m1).toArray());
<a name="line213"></a>}
<a name="line214"></a>
<a name="line215"></a>
<a name="line216"></a>function testSubtract1() {
<a name="line217"></a>  var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line218"></a>  var m2 = new goog.math.Matrix([[5, 5, 5], [5, 5, 5], [5, 5, 5]]);
<a name="line219"></a>
<a name="line220"></a>  assertArrayEquals([[-4, -3, -2], [-1, 0, 1], [2, 3, 4]],
<a name="line221"></a>                    m1.subtract(m2).toArray());
<a name="line222"></a>  assertArrayEquals([[4, 3, 2], [1, 0, -1], [-2, -3, -4]],
<a name="line223"></a>                    m2.subtract(m1).toArray());
<a name="line224"></a>}
<a name="line225"></a>
<a name="line226"></a>
<a name="line227"></a>function testSubtract2() {
<a name="line228"></a>  var m1 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line229"></a>  var m2 = new goog.math.Matrix([[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]]);
<a name="line230"></a>  assertArrayEquals([[2, 4, 6], [8, 10, 12], [14, 16, 18]],
<a name="line231"></a>                    m1.subtract(m2).toArray());
<a name="line232"></a>  assertArrayEquals([[-2, -4, -6], [-8, -10, -12], [-14, -16, -18]],
<a name="line233"></a>                    m2.subtract(m1).toArray());
<a name="line234"></a>}
<a name="line235"></a>
<a name="line236"></a>
<a name="line237"></a>function testScalarMultiplication() {
<a name="line238"></a>  var m1 = new goog.math.Matrix([[1, 1, 1], [2, 2, 2], [3, 3, 3]]);
<a name="line239"></a>  assertArrayEquals(
<a name="line240"></a>      [[2, 2, 2], [4, 4, 4], [6, 6, 6]], m1.multiply(2).toArray());
<a name="line241"></a>  assertArrayEquals(
<a name="line242"></a>      [[3, 3, 3], [6, 6, 6], [9, 9, 9]], m1.multiply(3).toArray());
<a name="line243"></a>  assertArrayEquals(
<a name="line244"></a>      [[4, 4, 4], [8, 8, 8], [12, 12, 12]], m1.multiply(4).toArray());
<a name="line245"></a>
<a name="line246"></a>  var m2 = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line247"></a>  assertArrayEquals(
<a name="line248"></a>      [[2, 4, 6], [8, 10, 12], [14, 16, 18]], m2.multiply(2).toArray());
<a name="line249"></a>}
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>function testMatrixMultiplication() {
<a name="line253"></a>  var m1 = new goog.math.Matrix([[1, 2], [3, 4]]);
<a name="line254"></a>  var m2 = new goog.math.Matrix([[3, 4], [5, 6]]);
<a name="line255"></a>  // m1 * m2
<a name="line256"></a>  assertArrayEquals([[1 * 3 + 2 * 5, 1 * 4 + 2 * 6],
<a name="line257"></a>                     [3 * 3 + 4 * 5, 3 * 4 + 4 * 6]],
<a name="line258"></a>                    m1.multiply(m2).toArray());
<a name="line259"></a>  // m2 * m1 != m1 * m2
<a name="line260"></a>  assertArrayEquals([[3 * 1 + 4 * 3, 3 * 2 + 4 * 4],
<a name="line261"></a>                     [5 * 1 + 6 * 3, 5 * 2 + 6 * 4]],
<a name="line262"></a>                    m2.multiply(m1).toArray());
<a name="line263"></a>  var m3 = new goog.math.Matrix([[1, 2, 3, 4],
<a name="line264"></a>                                 [5, 6, 7, 8]]);
<a name="line265"></a>  var m4 = new goog.math.Matrix([[1, 2, 3],
<a name="line266"></a>                                 [4, 5, 6],
<a name="line267"></a>                                 [7, 8, 9],
<a name="line268"></a>                                 [10, 11, 12]]);
<a name="line269"></a>  // m3 * m4
<a name="line270"></a>  assertArrayEquals([[1 * 1 + 2 * 4 + 3 * 7 + 4 * 10,
<a name="line271"></a>                      1 * 2 + 2 * 5 + 3 * 8 + 4 * 11,
<a name="line272"></a>                      1 * 3 + 2 * 6 + 3 * 9 + 4 * 12],
<a name="line273"></a>  [5 * 1 + 6 * 4 + 7 * 7 + 8 * 10,
<a name="line274"></a>   5 * 2 + 6 * 5 + 7 * 8 + 8 * 11,
<a name="line275"></a>   5 * 3 + 6 * 6 + 7 * 9 + 8 * 12]],
<a name="line276"></a>  m3.multiply(m4).toArray());
<a name="line277"></a>  assertThrows(&#39;Matrix dimensions should not line up.&#39;,
<a name="line278"></a>               function() { m4.multiply(m3); });
<a name="line279"></a>}
<a name="line280"></a>
<a name="line281"></a>function testMatrixMultiplicationIsAssociative() {
<a name="line282"></a>  var A = new goog.math.Matrix([[1, 2], [3, 4]]);
<a name="line283"></a>  var B = new goog.math.Matrix([[3, 4], [5, 6]]);
<a name="line284"></a>  var C = new goog.math.Matrix([[2, 7], [9, 1]]);
<a name="line285"></a>
<a name="line286"></a>  assertArrayEquals(&#39;A(BC) == (AB)C&#39;,
<a name="line287"></a>                    A.multiply(B.multiply(C)).toArray(),
<a name="line288"></a>                    A.multiply(B).multiply(C).toArray());
<a name="line289"></a>}
<a name="line290"></a>
<a name="line291"></a>
<a name="line292"></a>function testMatrixMultiplicationIsDistributive() {
<a name="line293"></a>  var A = new goog.math.Matrix([[1, 2], [3, 4]]);
<a name="line294"></a>  var B = new goog.math.Matrix([[3, 4], [5, 6]]);
<a name="line295"></a>  var C = new goog.math.Matrix([[2, 7], [9, 1]]);
<a name="line296"></a>
<a name="line297"></a>  assertArrayEquals(&#39;A(B + C) = AB + AC&#39;,
<a name="line298"></a>                    A.multiply(B.add(C)).toArray(),
<a name="line299"></a>                    A.multiply(B).add(A.multiply(C)).toArray());
<a name="line300"></a>
<a name="line301"></a>  assertArrayEquals(&#39;(A + B)C = AC + BC&#39;,
<a name="line302"></a>                    A.add(B).multiply(C).toArray(),
<a name="line303"></a>                    A.multiply(C).add(B.multiply(C)).toArray());
<a name="line304"></a>}
<a name="line305"></a>
<a name="line306"></a>
<a name="line307"></a>function testTranspose() {
<a name="line308"></a>  var m = new goog.math.Matrix([[1, 3, 1], [0, -6, 0]]);
<a name="line309"></a>  var t = [[1, 0], [3, -6], [1, 0]];
<a name="line310"></a>  assertArrayEquals(t, m.getTranspose().toArray());
<a name="line311"></a>}
<a name="line312"></a>
<a name="line313"></a>
<a name="line314"></a>function testAppendColumns() {
<a name="line315"></a>  var m = new goog.math.Matrix([[1, 3, 2], [2, 0, 1], [5, 2, 2]]);
<a name="line316"></a>  var b = new goog.math.Matrix([[4], [3], [1]]);
<a name="line317"></a>  var result = [[1, 3, 2, 4], [2, 0, 1, 3], [5, 2, 2, 1]];
<a name="line318"></a>  assertArrayEquals(result, m.appendColumns(b).toArray());
<a name="line319"></a>}
<a name="line320"></a>
<a name="line321"></a>
<a name="line322"></a>function testAppendRows() {
<a name="line323"></a>  var m = new goog.math.Matrix([[1, 3, 2], [2, 0, 1], [5, 2, 2]]);
<a name="line324"></a>  var b = new goog.math.Matrix([[4, 3, 1]]);
<a name="line325"></a>  var result = [[1, 3, 2], [2, 0, 1], [5, 2, 2], [4, 3, 1]];
<a name="line326"></a>  assertArrayEquals(result, m.appendRows(b).toArray());
<a name="line327"></a>}
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>function testSubmatrixByDeletion() {
<a name="line331"></a>  var m = new goog.math.Matrix([[1, 2, 3, 4], [5, 6, 7, 8],
<a name="line332"></a>        [9, 10, 11, 12], [13, 14, 15, 16]]);
<a name="line333"></a>  var arr = [[1, 2, 3], [5, 6, 7], [13, 14, 15]];
<a name="line334"></a>  assertArrayEquals(arr, m.getSubmatrixByDeletion_(2, 3).toArray());
<a name="line335"></a>}
<a name="line336"></a>
<a name="line337"></a>
<a name="line338"></a>function testMinor() {
<a name="line339"></a>  var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line340"></a>  assertEquals(-3, m.getMinor_(0, 0));
<a name="line341"></a>}
<a name="line342"></a>
<a name="line343"></a>
<a name="line344"></a>function testCofactor() {
<a name="line345"></a>  var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line346"></a>  assertEquals(6, m.getCofactor_(0, 1));
<a name="line347"></a>}
<a name="line348"></a>
<a name="line349"></a>
<a name="line350"></a>function testDeterminantForOneByOneMatrix() {
<a name="line351"></a>  var m = new goog.math.Matrix([[3]]);
<a name="line352"></a>  assertEquals(3, m.getDeterminant());
<a name="line353"></a>}
<a name="line354"></a>
<a name="line355"></a>
<a name="line356"></a>function testDeterminant() {
<a name="line357"></a>  var m = new goog.math.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
<a name="line358"></a>  assertEquals(0, m.getDeterminant());
<a name="line359"></a>}
<a name="line360"></a>
<a name="line361"></a>
<a name="line362"></a>function testGetSubmatrix() {
<a name="line363"></a>  var m = new goog.math.Matrix([[2, -1, 0, 1, 0, 0],
<a name="line364"></a>                                [-1, 2, -1, 0, 1, 0],
<a name="line365"></a>                                [0, -1, 2, 0, 0, 1]]);
<a name="line366"></a>  var sub1 = [[2, -1, 0], [-1, 2, -1], [0, -1, 2]];
<a name="line367"></a>  assertArrayEquals(sub1,
<a name="line368"></a>                    m.getSubmatrixByCoordinates_(0, 0, 2, 2).toArray());
<a name="line369"></a>
<a name="line370"></a>  var sub2 = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
<a name="line371"></a>  assertArrayEquals(sub2, m.getSubmatrixByCoordinates_(0, 3).toArray());
<a name="line372"></a>}
<a name="line373"></a>
<a name="line374"></a>
<a name="line375"></a>function testGetReducedRowEchelonForm() {
<a name="line376"></a>  var m = new goog.math.Matrix([[2, -1, 0, 1, 0, 0],
<a name="line377"></a>                                [-1, 2, -1, 0, 1, 0],
<a name="line378"></a>                                [0, -1, 2, 0, 0, 1]]);
<a name="line379"></a>
<a name="line380"></a>  var expected = new goog.math.Matrix([[1, 0, 0, .75, .5, .25],
<a name="line381"></a>                                       [0, 1, 0, .5, 1, .5],
<a name="line382"></a>                                       [0, 0, 1, .25, .5, .75]]);
<a name="line383"></a>
<a name="line384"></a>  assertTrue(expected.equals(m.getReducedRowEchelonForm()));
<a name="line385"></a>}
<a name="line386"></a>
<a name="line387"></a>
<a name="line388"></a>function testInverse() {
<a name="line389"></a>  var m1 = new goog.math.Matrix([[2, -1, 0],
<a name="line390"></a>                                 [-1, 2, -1],
<a name="line391"></a>                                 [0, -1, 2]]);
<a name="line392"></a>  var expected1 = new goog.math.Matrix([[.75, .5, .25],
<a name="line393"></a>                                        [.5, 1, .5],
<a name="line394"></a>                                        [.25, .5, .75]]);
<a name="line395"></a>  assertTrue(expected1.equals(m1.getInverse()));
<a name="line396"></a>
<a name="line397"></a>  var m2 = new goog.math.Matrix([[4, 8],
<a name="line398"></a>                                 [7, -2]]);
<a name="line399"></a>  var expected2 = new goog.math.Matrix([[.03125, .125],
<a name="line400"></a>                                        [.10936, -.0625]]);
<a name="line401"></a>  assertTrue(expected2.equals(m2.getInverse(), .0001));
<a name="line402"></a>  var m3 = new goog.math.Matrix([[0, 0],
<a name="line403"></a>                                 [0, 0]]);
<a name="line404"></a>  assertNull(m3.getInverse());
<a name="line405"></a>  var m4 = new goog.math.Matrix([[2]]);
<a name="line406"></a>  var expected4 = new goog.math.Matrix([[.5]]);
<a name="line407"></a>  assertTrue(expected4.equals(m4.getInverse(), .0001));
<a name="line408"></a>  var m5 = new goog.math.Matrix([[0]]);
<a name="line409"></a>  assertNull(m5.getInverse());
<a name="line410"></a>}
<a name="line411"></a>
<a name="line412"></a>
<a name="line413"></a>function testEquals() {
<a name="line414"></a>  var a1 = new goog.math.Matrix([[1, 0, 0, .75, .5, .25],
<a name="line415"></a>                                 [0, 1, 0, .5, 1, .5],
<a name="line416"></a>                                 [0, 0, 1, .25, .5, .75]]);
<a name="line417"></a>
<a name="line418"></a>  var a2 = new goog.math.Matrix([[1, 0, 0, .75, .5, .25],
<a name="line419"></a>                                 [0, 1, 0, .5, 1, .5],
<a name="line420"></a>                                 [0, 0, 1, .25, .5, .75]]);
<a name="line421"></a>
<a name="line422"></a>  var a3 = new goog.math.Matrix([[1, 0, 0, .749, .5, .25],
<a name="line423"></a>                                 [0, 1, 0, .5, 1, .5],
<a name="line424"></a>                                 [0, 0, 1, .25, .5, .75]]);
<a name="line425"></a>
<a name="line426"></a>  assertTrue(a1.equals(a2));
<a name="line427"></a>  assertTrue(a1.equals(a3, .01));
<a name="line428"></a>  assertFalse(a1.equals(a3, .001));
<a name="line429"></a>}
</pre>


</body>
</html>
